#!/bin/bash

echo "=== Prometheus 测试开始 ==="

# 启动 Prometheus 服务
echo "启动 Prometheus 服务..."
docker-compose -f prometheus-compose.yml up -d

# 等待 Prometheus 启动
echo "等待 Prometheus 启动..."
sleep 15

# 检查容器状态
if ! docker ps | grep -q "test-prometheus"; then
    echo "❌ Prometheus 容器启动失败"
    exit 1
fi

echo "✅ Prometheus 容器启动成功"

# 测试健康检查
echo "测试健康检查..."
if curl -s http://localhost:9090/-/healthy | grep -q "Prometheus Server is Healthy"; then
    echo "✅ 健康检查通过"
else
    echo "❌ 健康检查失败"
    exit 1
fi

# 测试就绪状态
echo "测试就绪状态..."
if curl -s http://localhost:9090/-/ready | grep -q "Prometheus Server is Ready"; then
    echo "✅ 就绪状态正常"
else
    echo "❌ 就绪状态异常"
    exit 1
fi

# 测试配置重载
echo "测试配置重载..."
if curl -s -X POST http://localhost:9090/-/reload | grep -q ""; then
    echo "✅ 配置重载接口正常"
else
    echo "❌ 配置重载接口异常"
fi

# 测试 Targets API
echo "测试 Targets API..."
TARGETS_RESPONSE=$(curl -s http://localhost:9090/api/v1/targets)
if echo "$TARGETS_RESPONSE" | grep -q '"status":"success"'; then
    echo "✅ Targets API 正常"
    # 计算活跃目标数量
    ACTIVE_COUNT=$(echo "$TARGETS_RESPONSE" | grep -o '"health":"up"' | wc -l)
    echo "活跃目标数量: $ACTIVE_COUNT"
else
    echo "❌ Targets API 异常"
    exit 1
fi

# 测试 Labels API
echo "测试 Labels API..."
if curl -s http://localhost:9090/api/v1/labels | grep -q '"status":"success"'; then
    echo "✅ Labels API 正常"
else
    echo "❌ Labels API 异常"
    exit 1
fi

# 测试查询 API
echo "测试查询 API..."
QUERY_RESPONSE=$(curl -s "http://localhost:9090/api/v1/query?query=up")
if echo "$QUERY_RESPONSE" | grep -q '"status":"success"'; then
    echo "✅ 查询 API 正常"
    # 显示查询结果数量
    RESULT_COUNT=$(echo "$QUERY_RESPONSE" | grep -o '"metric":' | wc -l)
    echo "查询结果数量: $RESULT_COUNT"
else
    echo "❌ 查询 API 异常"
    exit 1
fi

# 测试范围查询 API
echo "测试范围查询 API..."
END_TIME=$(date +%s)
START_TIME=$((END_TIME - 3600))  # 1小时前
RANGE_QUERY="http://localhost:9090/api/v1/query_range?query=up&start=${START_TIME}&end=${END_TIME}&step=60"
if curl -s "$RANGE_QUERY" | grep -q '"status":"success"'; then
    echo "✅ 范围查询 API 正常"
else
    echo "❌ 范围查询 API 异常"
fi

# 测试指标 API
echo "测试指标 API..."
METRICS_COUNT=$(curl -s http://localhost:9090/api/v1/label/__name__/values | grep -o '"[^"]*"' | wc -l)
if [ "$METRICS_COUNT" -gt 0 ]; then
    echo "✅ 指标 API 正常，发现 $METRICS_COUNT 个指标"
else
    echo "❌ 指标 API 异常"
fi

# 检查存储状态
echo "检查存储状态..."
STORAGE_RESPONSE=$(curl -s http://localhost:9090/api/v1/status/tsdb)
if echo "$STORAGE_RESPONSE" | grep -q '"status":"success"'; then
    echo "✅ 存储状态正常"
else
    echo "❌ 存储状态异常"
fi

# 检查配置
echo "检查配置..."
CONFIG_RESPONSE=$(curl -s http://localhost:9090/api/v1/status/config)
if echo "$CONFIG_RESPONSE" | grep -q '"status":"success"'; then
    echo "✅ 配置状态正常"
else
    echo "❌ 配置状态异常"
fi

# 性能测试 - 查询延迟
echo "执行查询性能测试..."
START_TIME=$(date +%s%N)
curl -s "http://localhost:9090/api/v1/query?query=prometheus_notifications_total" > /dev/null
END_TIME=$(date +%s%N)
LATENCY=$(( (END_TIME - START_TIME) / 1000000 ))  # 转换为毫秒
echo "查询延迟: ${LATENCY}ms"

echo "=== Prometheus 测试完成 ==="

# 选择是否停止服务
read -p "是否停止 Prometheus 服务? (y/n): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
    docker-compose -f prometheus-compose.yml down
    echo "Prometheus 服务已停止"
fi